<?php
include './include/common.inc.php';
include './include/investigate.class.php';
require APP_ROOT.'include/form.class.php';

$investigate_db = new investigate();
$action = isset( $action ) ? $action : 'manage';
$forward = get_forward( 'investigate.php' );
$site_id = isset($site_id) ? $site_id : $site_conf->get('SITE_ID');

$params = array();
array_push( $params, "status = 99" );
if( isset( $name ) && strlen( $name ) > 0 )
{
    array_push( $params, "name like '%$name%'" );
}
$order = " ORDER BY listorder DESC, inputtime DESC";

if( $current_user === false )
{
	if( isset( $edufepm ) && $edufepm == 'iframe' )
	{
		$response->alert( '请您登陆后再访问' );
		$response->redirect( $base_dir . 'admin/login.php' );
		$response->execute();
	}
	else
	{
		showmessage( '请您登陆后再访问', $base_dir . 'admin/login.php' );
	}
}

switch( $action )
{
    case 'add':
        $have_perm = check_perms( 'investigate002' );
        if( isset( $_POST['name'] ) )
		{
            if( $have_perm === false )
			{
				$response->alert( '您没有权限进行此操作！' );
				$response->execute();
			}
			if( empty( $name ) || strlen( $name ) > 100 )
			{
				$response->text_alert( 'error','name','调查问卷名称不能为空且长度不能大于100个字符' );
			}
			$response->real_execute();
			$params = array(
				'name'	      =>  $name,
                'thumb'       =>  $thumb,
                'description' =>  $description,
                'units'       =>  $units,
                'startdate'   =>  $startdate,
                'enddate'     =>  $enddate,
                'status'      =>  99,
                'username'    =>  $_SESSION['current_user']['username'],
                'inputtime'   =>  time(),
                'siteid'      =>  $site_id,
			);
            $res = $investigate_db->add( $params );
			if( !empty($res) )
			{
				$response->alert( '添加成功' );
				$response->redirect( urldecode( $forward ) );
			}
			else
			{
				$response->alert( '添加失败' );
				$response->script( 'self.location = self.location' );
			}
			$response->execute();
		}
		else
		{
            if( $have_perm === false )
			{
				showmessage('您没有权限进行此操作！');
			}
			include template( 'admin', 'investigate_add' );
		}
        break;

    case 'edit':
        $have_perm = check_perms( 'investigate003' );
        if( isset( $_POST['name'] ) )
		{
            if( $have_perm === false )
			{
				$response->alert( '您没有权限进行此操作！' );
				$response->execute();
			}
			if( empty( $name ) || strlen( $name ) > 100 )
			{
				$response->text_alert( 'error','name','调查问卷名称不能为空且长度不能大于100个字符' );
			}
			$response->real_execute();
			$params = array(
				'name'	      =>  $name,
                'thumb'       =>  $thumb,
                'description' =>  $description,
                'units'       =>  $units,
                'startdate'   =>  $startdate,
                'enddate'     =>  $enddate,
                'editor'      =>  $_SESSION['current_user']['username'],
                'edittime'    =>  time(),
			);
            $res = $investigate_db->edit( $params, $id );
			if( !empty($res) )
			{
				$response->alert( '修改成功' );
				$response->redirect( urldecode( $forward ) );
			}
			else
			{
				$response->alert( '修改失败' );
				$response->script( 'self.location = self.location' );
			}
			$response->execute();
		}
		else
		{
            if( $have_perm === false )
			{
				showmessage('您没有权限进行此操作！');
			}
            $info = $investigate_db->get( $id );
			if( !isset( $info['id'] ) )
			{
				showmessage( '要编辑的调查问卷不存在' );
			}
			include template( 'admin', 'investigate_upd' );
		}
        break;

    case 'del':
        $have_perm = check_perms( 'investigate004' );
        if( $have_perm === false )
        {
            $response->alert( '您没有权限进行此操作！' );
            $response->execute();
        }
        $info = $investigate_db->get( $id );
		if( !isset( $info['id'] ) )
		{
			$response->alert( '要删除的调查问卷不存在' );
			$response->execute();
		}
        $res = $investigate_db->del( $id );
		if( $res == false )
		{
			$response->alert( '删除失败' );
		}
		else
		{
			$response->alert( '删除成功' );
			$response->script( 'self.location=self.location' );
		}
		$response->execute();
        break;

    case 'del_all':
        $have_perm = check_perms( 'investigate004' );
        if( $have_perm === false )
        {
            $response->alert( '您没有权限进行此操作！' );
            $response->execute();
        }
        if( isset( $_POST['iid'] ) )
		{
			foreach ( $iid as $id )
			{
				$investigate_db->del( $id );
			}
			$response->alert( '删除成功！' );
			$response->script( 'self.location=self.location' );
			$response->execute();
		}
		else
		{
			$response->alert( '请选择要删除的调查问卷' );
			$response->execute();
		}
        break;

    case 'listorder':
        $have_perm = check_perms( 'investigate005' );
        if( $have_perm === false )
        {
            $response->alert( '您没有权限进行此操作！' );
            $response->execute();
        }
        $result = $investigate_db->listorder( $listorder );
		if( $result )
		{
            $response->alert( '修改成功' );
            $response->script( 'self.location=self.location' );
		}
		else
		{
			$response->alert( '修改失败' );
		}
        $response->execute();
        break;

    case 'search':
        $url = 'investigate.php';
		$url .= isset( $name ) && !empty( $name ) ? '?name='.urlencode( $name ) : '';
		$response->redirect( $url );
		$response->execute();
        break;

    case 'manage':

    default:
        $have_perm = check_perms( 'investigate001' );
        if( $have_perm === false )
        {
            showmessage( '您没有权限进行此操作！' );
        }
        $page = isset( $page ) ? intval( $page ) : 1;
		$page = max( $page, 1 );
        $pagesize = 10;
        $params[] = "siteid='{$site_id}'";
		$investigate_list = $investigate_db->get_list( $params, $order, $page, $pagesize );
        $investigate_pages = $investigate_db->pages;
        $forward = make_forward();
		include template( 'admin', 'investigate_manage' );
        break;
}

?>
